﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;



namespace Howork.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            String name = LoginUser.UserName;
            String pass = LoginUser.Password;
            String strConnection = "server=.\\SQLEXPRESS;database=howork_c;uid=yuleo;pwd=yuleoyubin;";
            SqlConnection Connection = new SqlConnection(strConnection);
            String strSQL = "Select * From employee where id ='" + name + "' and password = '" + pass + "'";
            SqlCommand command = new SqlCommand(strSQL, Connection);
            Connection.Open();
            SqlDataReader Dr;
            Dr = command.ExecuteReader();
            if(Dr.HasRows)
            {
                Dr.Read();
                Session["login"] = "login";  //是否登录 
                Session["id"] = Dr["id"].ToString();
                Session["name"] = Dr["name"].ToString();
                Session["department_id"] = Dr["department_id"].ToString();
                Session["station_id"] = Dr["station_id"].ToString();
                WorkFlowURL.userID = Dr["id"].ToString();


                //默认的初始工作
                Session["work_type"] = WorkType.gwlzwxsp;
                


                //得到职位信息
                String getStationStr = "Select * From station where id = '" + Dr["station_id"] + "'";
                Dr.Close();
                SqlCommand anotherCommand = new SqlCommand(getStationStr, Connection);
                SqlDataReader anotherDr;
                anotherDr = anotherCommand.ExecuteReader();
                if(anotherDr.HasRows)
                {
                    anotherDr.Read();
                    Session["station_grade"] = anotherDr["grade"].ToString();
                }
                else
                {
                    Session["station_grade"] = "NULL";
                }
                


                
                anotherDr.Close();
                Connection.Close();

                //Server.Transfer("~/Welcome.aspx");
                Response.Redirect("~/Welcome.aspx");
            }
            else
            {
                LoginUser.FailureText = "登录失败！";
                Connection.Close();
            }
           
        }
    }
}
